package com.chf.controller;

import com.chf.bean.UserInfoBean;
import com.chf.dao.UserInfoBeanMapper;
import com.chf.service.IUserService;
import com.chf.utils.MyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
public class UserControler {
    @Resource
    private IUserService userService;
    @Resource
    HttpServletRequest request;

    //登录 使用request方法设置属性值
    @RequestMapping("/login")
    public String doLogin(UserInfoBean userInfo) {
        System.out.println("正在进行登录操作:" + userInfo.getUsername() + ";密码：" + userInfo.getUserpsw());
        boolean canLogin = userService.doLogin(userInfo);
        // 根据查询结果集进行跳转
        if (canLogin) {
            // 登录成功
            // 返回的视图的路径：WEB-INF/pages/logined.jsp
            // 通过springmvc的视图解析器：会自动附加 前缀 /WEB-INF/pages/ 和后缀 .jsp，所以只需要返回中间部分即可；
            request.setAttribute("userName",userInfo.getUsername());
            return "login";
        } else {
            // 登录失败，跳回登录页面
            return "../../index";
        }
    }
    //登录 使用视图解析器来设置属性值
    @RequestMapping("/login2")
    public ModelAndView doLogin2(UserInfoBean userInfo) {
        ModelAndView mav = new ModelAndView();
        System.out.println(userInfo);
        boolean canLogin = userService.doLogin(userInfo);
        // 根据查询结果集进行跳转
        if (canLogin) {
            // 登录成功
            // 返回的视图的路径：WEB-INF/pages/logined.jsp
            // 通过springmvc的视图解析器：会自动附加 前缀 /WEB-INF/pages/ 和后缀 .jsp，所以只需要返回中间部分即可；
//            设置视图解析器的参数

            mav.setViewName("login");
            //设置一个值，用于前台接收
            mav.addObject("userName", userInfo.getUsername());
        } else {
            // 登录失败，跳回登录页面
            mav.setViewName("../../index");
        }
        return mav;
    }
//    跳转
    @RequestMapping("/jump")
    public String jump(String pg){
        return pg;
    }
    //直接查看所有用户信息 跳转mapping
    @RequestMapping("/jump_AllInfo")
    public String jumpAllInfo(String pg){
        request.getSession().setAttribute("userInfoList",userService.selectAllInfo());
        return pg;
    }
    //添加用户
    @RequestMapping("/UserInfoAdd")
    public String UserInfoAdd(UserInfoBean userInfoBean,@RequestParam("file_userimg") MultipartFile multipartFile){
        ////////////////文件处理操作////////////////
        MyUtils.doSingleFileUpload(multipartFile,"uploadfiles","userimg",userInfoBean,"userimg");
        ////////////////插入数据操作////////////////
        int i = userService.insertSelective(userInfoBean);
        if(i>0){
            List<UserInfoBean> userlist = userService.selectAllInfo();
            request.getSession().setAttribute("userInfoList",userlist);
            return "userInfo/SelectAllUserInfo";
        }else {
            return "userInfo/UserInfoAdd";
        }
    }

    public IUserService getUserService() {
        return userService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
}
